var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
let time = require("time-stamp");
let session = require("express-session");

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
let options = {
  secret:"offcn.com", //密钥字符串
  resave:false,
  saveUninitialized:true
}
app.use( session(options) );
app.use(express.static(path.join(__dirname, 'public')));
app.set("view engine","ejs");

app.use( (req,res,next)=>{
  req.time = time("YYYY年MM月DD日 HH:mm:ss")
  next();
})

app.use('/', indexRouter);
app.use('/users', usersRouter);

// 逻辑错误消息处理中间件
app.use((err,req,res,next)=>{
  res.render("error.ejs",{"err":err});
})

module.exports = app;
